package edu.kaist.cs408.pof.backend.dao;

import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations;
import edu.kaist.cs408.pof.common.model.DishVo;
import org.bson.types.ObjectId;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * User: duyleekun
 * Date: 5/10/12
 * Time: 12:42 AM
 * To change this template use File | Settings | File Templates.
 */
@Configuration
public class DishDaoImpl extends DaoBase {
	public DishVo createDish(DishVo newDish) {
		datastore.save(newDish);
		return newDish;
	}

	public DishVo deleteDish(ObjectId dish) {
		return datastore.findAndDelete(datastore.createQuery(DishVo.class).field("_id").equal(dish));
	}

	public DishVo rate(ObjectId dish, int score) {
		Query<DishVo> queryOp = datastore.createQuery(DishVo.class).field("_id").equal(dish);
		UpdateOperations<DishVo> voteOp = datastore.createUpdateOperations(DishVo.class).inc("countReview", score).inc("totalReviewScore", 1);


		return datastore.findAndModify(queryOp, voteOp);
	}

	public DishVo get(ObjectId dish) {
		return datastore.get(DishVo.class, dish);
	}
}
